package org.goochasca

import java.lang.Math._
import java.awt.Color

object Implicits {
  implicit def pimpColor(color: Color) = new PimpedColor(color)
}

class PimpedColor(c: Color) {
  val r = c.getRed
  val g = c.getGreen
  val b = c.getBlue
  val a = c.getAlpha
  
  private val alphaFactor = a / 255.0f
  private val ar: Int = round(alphaFactor * r)
  private val ag: Int = round(alphaFactor * g)
  private val ab: Int = round(alphaFactor * b)
  
  private def limit(channel: Int) = min(255, channel)
  
  def alpha(alpha: Int) = new Color(r, g, b, alpha)
  def +(that: PimpedColor) = new Color(this.ar+that.ar/2, this.ag+that.ag, this.ab+that.ab/2, this.a+that.a/2)
  def *(that: PimpedColor) = new Color(limit(this.ar+that.ar), limit(this.ag+that.ag), limit(this.ab+that.ab), (this.a+that.a)/2)
}
